/*
	Copyright (c) 2010, Mathias Myrland, Myrland Systemutvikling ( mathias@myrsys.no)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	      
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	      
	    * Neither the name of Myrland Systemutvikling nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cgf/enginetypes.hpp>
#include <cgf/componentfactory.hpp>
#include <cgf/componenttemplate.hpp>
#include <cgf/error.hpp>

#include <tinyxml/tinyxml.h>

using namespace std;
using namespace CGF;


//	Load the template specifications from an xml file
void ComponentFactory::loadTemplates ( string filename )
{
	
	TiXmlNode* rootnode, *classnode;
	string name, base, cname;
	
	//	Create an xml file reader
	TiXmlDocument file ( filename );
	if ( !file.LoadFile () )
		throw Error ( "Could not load the component template file " + filename + "." );
	
	rootnode = file.FirstChildElement ( "templates" );
	rootnode = rootnode->FirstChildElement ( "baseComponent" );
	
	//	Read all the template specifications
	while ( rootnode )
	{
		
		
		try
		{
			name = GetAttribute ( rootnode, "name" );
		}
		catch ( string& s ) { throw Error ( s ); }
		try
		{
			base = GetAttribute ( rootnode, "base" );
		}
		catch ( string& s ) { base = ""; }
		
		//	Handle inheritance
		if ( base == "" )
		{
			//	Create the new base component template
			ptr_ComponentTemplate created ( new ComponentTemplate () );
			m_templates[name] = created;
		}
		else
		{
			//	Create the new templated based on a previously declared one
			ptr_ComponentTemplate created ( new ComponentTemplate ( m_templates[base]) );
			m_templates[name] = created;
		}
		
		//	Read any possible extra class specifications
		classnode = rootnode->FirstChildElement ( "class" );
		while ( classnode )
		{
			try { cname = GetAttribute ( classnode, "name" ); }
			catch ( string& s ) { throw Error ( s ); }
			m_templates[name]->addClass ( cname );
			
			classnode = classnode->NextSibling ( "class" );
		}
		
		rootnode = rootnode->NextSibling ( "baseComponent" );
	}
}

//	The creation function of the factory
ptr_Component CGF::ComponentFactory::createComponent ( string name, ptr_Entity entity)
{
	//	First some error checking
	if ( m_templates.count ( name ) == 0 )
		throw Error ( "Component template " + name + " does not exist" );
	
	//	Utilize the copy constructor and create the new component
	ptr_ComponentTemplate temp = m_templates[name];
	return temp->createComponent ( temp, entity );
}


//	Basic constructor
ComponentFactory::ComponentFactory()
{
}

ComponentFactory::~ComponentFactory()
{
	m_templates.clear();
}
